package com.example.myapplication;

import com.example.myapplication.shortvideo.utils.GsonUtil;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class JinPai {
    int index;
    List<Bean> mList = new ArrayList<>();
    public static void main(String[] args) {
        new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    JinPai jinPai = new JinPai();
                    jinPai.query();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }, 0, 60 * 1000);
    }

    public static class Cos {
//        public void init() {
//// 存储桶所在地域简称，例如广州地区是 ap-guangzhou
//            String region = "COS_REGION";
//// 创建 CosXmlServiceConfig 对象，根据需要修改默认的配置参数
//            CosXmlServiceConfig serviceConfig = new CosXmlServiceConfig.Builder()
//                    .setRegion(region)
//                    .isHttps(true) // 使用 HTTPS 请求, 默认为 HTTP 请求
//                    .builder();
//// 初始化 COS Service，获取实例
//            CosXmlService cosXmlService = new CosXmlService(context,serviceConfig, myCredentialProvider);
//        }
    }

    public void query() {
        String url = "https://tiyu.baidu.com/tokyoly/home/tab/%E5%A5%96%E7%89%8C%E6%A6%9C";
        OkHttpClient okHttpClient = new OkHttpClient();
        final Request request = new Request.Builder()
                .url(url)
                .get()//默认就是GET请求，可以不写
                .build();
        Call call = okHttpClient.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String str = response.body().string();
                fenxi(str);
                save();
//                upLoad();
            }
        });
    }


    public void save() {
        String json = GsonUtil.toJson(mList);
        try {

            File file = new File("D:\\downloads\\putjson\\rank.json");

            //if file doesnt exists, then create it
            if (file.exists()) {
                file.delete();
            }
            file.createNewFile();

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(json);
            bw.close();

            System.out.println("保存成功");

        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public void fenxi(String html) {

        int idex1 = html.indexOf("class=\"country-img\"");
        if (idex1 <= 0) {
            return;
        }
        String htm11 = html.substring(idex1);
        String tag2 = "background:url(";
        int index2 = htm11.indexOf(tag2);
        String html2 = htm11.substring(index2 + tag2.length());
        String tag3 = ")";
        int index3 = html2.indexOf(tag3);
        String html3 = html2.substring(0, index3);

        String tag4 = "class=\"name\"";
        int index4 = html2.indexOf(tag4);
        String html4 = html2.substring(index4);

        String tag5 = ">";
        int index5 = html4.indexOf(tag5);
        String html5 = html4.substring(index5 + tag5.length());

        String tag6 = "</span>";
        int index6 = html5.indexOf(tag6);
        String html6 = html5.substring(0, index6);


        String tag7 = "class=\"item-gold";
        int index7 = html5.indexOf(tag7);
        String html7 = html5.substring(index7);

        String tag8 = ">";
        int index8 = html7.indexOf(tag8);
        String html8 = html7.substring(index8 + tag8.length());

        String tag9 = "</div>";
        int index9 = html8.indexOf(tag9);
        String html9 = html8.substring(0, index9);


        String tag10 = "class=\"item-silver";
        int index10 = html8.indexOf(tag10);
        String html10 = html8.substring(index10);

        String tag11 = ">";
        int index11 = html10.indexOf(tag11);
        String html11 = html10.substring(index11 + tag11.length());

        String tag12 = "</div>";
        int index12 = html11.indexOf(tag12);
        String html12 = html11.substring(0, index12);


        String tag13 = "class=\"item-copper";
        int index13 = html10.indexOf(tag13);
        String html13 = html10.substring(index13);

        String tag14 = ">";
        int index14 = html13.indexOf(tag14);
        String html14 = html13.substring(index14 + tag14.length());

        String tag15 = "</div>";
        int index15 = html14.indexOf(tag15);
        String html15 = html14.substring(0, index15);


        String tag16 = "class=\"item-all";
        int index16 = html13.indexOf(tag16);
        String html16 = html13.substring(index16);

        String tag17 = ">";
        int index17 = html16.indexOf(tag17);
        String html17 = html16.substring(index17 + tag17.length());

        String tag18 = "</div>";
        int index18 = html17.indexOf(tag18);
        String html18 = html17.substring(0, index18);


        index++;
        Bean bean = new Bean();
        bean.rank = index;
        bean.imgUrl = html3.replace("\n", "").replace(" ", "");
        bean.name = html6.replace("\n", "").replace(" ", "");
        bean.gold = html9.replace("\n", "").replace(" ", "");
        bean.silver = html12.replace("\n", "").replace(" ", "");
        bean.copper = html15.replace("\n", "").replace(" ", "");
        bean.all = html18.replace("\n", "").replace(" ", "");

        mList.add(bean);
        System.out.println("rank:" + bean.rank + " name:" + " gold:" + bean.gold
                + " silver:" + bean.silver + " copper:" + bean.copper + " all:" + bean.all + bean.name + " imgUrl:" + bean.imgUrl);

        fenxi(html16);
    }

    public static class Bean {
        public int rank;
        public String imgUrl;
        public String name;
        public String gold;
        public String silver;
        public String copper;
        public String all;
    }
}
